import 'package:flutter_09_game/utils/log/utils_log.dart';

import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'base_controller.dart';

class BaseListController extends BaseController {
  List list = [];
  int pageNum = 1;
  int pageSize = 20;
  RefreshController refreshController = RefreshController();

  bool _initialRefresh = true;

  set initialRefresh(bool value) {
    clog("===> ${runtimeType.toString()} initialRefresh $value");
    _initialRefresh = value;
    refreshController = RefreshController(initialRefresh: value);
    if (!_initialRefresh) {
      // if (isNoNetWork) {
      //   doNoNetwork();
      // } else if (list.isEmpty == true) {
      doLoading();
      // }
      refreshList();
    }
  }

  bool get emptyViewRefresher => true;

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }

  Future<bool> refreshList() {
    clog('refreshList');

    return loadData().then((value) {
      // if (isNoNetWork) {
      //   // clog('1');
      //   if (list.isEmpty == true) {
      //     doNoNetwork();
      //   }
      //   refreshController.refreshFailed();
      // } else
      if (value.isEmpty) {
        // clog('2');
        list = value;
        doEmpty();
        refreshController.refreshCompleted(resetFooterState: false);
      } else {
        // clog('3');
        list = value;
        doFinished();
        refreshController.refreshCompleted(resetFooterState: false);
      }
      return true;
    }, onError: (error) {
      // if (isNoNetWork) {
      //   if (list.isEmpty == true) {
      //     doNoNetwork();
      //   }
      // } else {
      list = [];
      doError(error);
      // }

      refreshController.refreshFailed();
      return true;
    });
  }

  Future<List> loadData() async {
    // TODO: implement loadMoreList
    throw UnimplementedError("loadData not implement");
  }
}
